//
//  GQHReviewView.m
//  Xanthium
//
//  Created by GuanQinghao on 05/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHReviewView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"
#import "QHRatingView.h"

@interface GQHReviewView () <UITableViewDelegate,UITableViewDataSource> {
    
    BOOL visible;
    
    UIView *leftMarkView;
    UIView *rightMarkView;
}

@end

@implementation GQHReviewView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    [self loadHeaderView];
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.separatorColor = kLightGrayColor;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.mas_equalTo(self);
        make.top.mas_equalTo(self).with.offset(self.qh_statusBarHeight + self.qh_navigationBarHeight + 160);
        make.bottom.mas_equalTo(self).with.inset(self.qh_homeIndicatorHeight);
    }];
}

- (void)loadHeaderView {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kAppleGrayColor;
    [self addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.height.mas_equalTo(160);
    }];
    
    UIView *userInfoView = [[UIView alloc] init];
    userInfoView.backgroundColor = kAppRedColor;
    [headerView addSubview:userInfoView];
    [userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.top.and.right.mas_equalTo(headerView);
        make.height.mas_equalTo(100);
    }];
    
    /// 头像
    UIImageView *avatarImageView = [[UIImageView alloc] init];
    avatarImageView.layer.cornerRadius = 5.0f;
    avatarImageView.layer.masksToBounds = YES;
    NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:USER_GET_AVATAR];
    NSURL *URL = [NSURL URLWithString:URLString];
    [avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
    [userInfoView addSubview:avatarImageView];
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(userInfoView).with.offset(30);
        make.top.mas_equalTo(userInfoView).with.offset(20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
    }];
    
    UILabel *userNameLabel = [[UILabel alloc] init];
    userNameLabel.backgroundColor = kClearColor;
    userNameLabel.font = kBiggerFont;
    userNameLabel.text = USER_GET_NAME;
    userNameLabel.textColor = kWhiteColor;
    userNameLabel.textAlignment = NSTextAlignmentLeft;
    [userInfoView addSubview:userNameLabel];
    [userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(avatarImageView.mas_right).with.offset(20);
        make.top.mas_equalTo(avatarImageView);
        make.right.mas_equalTo(userInfoView).with.inset(30);
        make.height.mas_equalTo(40);
    }];
    
    UIView *starView = [[UIView alloc] init];
    starView.backgroundColor = kClearColor;
    [userInfoView addSubview:starView];
    [starView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(userNameLabel);
        make.top.mas_equalTo(userNameLabel.mas_bottom);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(20);
    }];
    
    CGRect frame = CGRectMake(0, 0, 100, 20);
    QHRatingView *ratingView = [[QHRatingView alloc] initWithFrame:frame];
    ratingView.backgroundColor = kClearColor;
    CGFloat score = [USER_GET_SCORE floatValue];
    [ratingView showStarWithValue:score];
    [starView addSubview:ratingView];
    
    UILabel *scoreLabel = [[UILabel alloc] init];
    scoreLabel.backgroundColor = kClearColor;
    scoreLabel.font = kSmallerFont;
    scoreLabel.text = [NSString stringWithFormat:@"%.2f 分",score];
    scoreLabel.textColor = kWhiteColor;
    scoreLabel.textAlignment = NSTextAlignmentLeft;
    [userInfoView addSubview:scoreLabel];
    [scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(starView.mas_right).with.offset(10);
        make.top.mas_equalTo(starView);
        make.right.mas_equalTo(userInfoView).with.inset(30);
        make.height.mas_equalTo(starView);
    }];
    
    _qh_commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _qh_commentButton.backgroundColor = kWhiteColor;
    _qh_commentButton.titleLabel.font = kDefaultFont;
    [_qh_commentButton setTitle:@"评价我的" forState:UIControlStateNormal];
    [_qh_commentButton setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    [_qh_commentButton addTarget:self action:@selector(displayCommentView:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_qh_commentButton];
    [_qh_commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(100);
        make.width.mas_equalTo(headerView).with.multipliedBy(0.5);
        make.height.mas_equalTo(50);
    }];
    
    leftMarkView = [[UIView alloc] init];
    leftMarkView.layer.cornerRadius = 1.0;
    leftMarkView.layer.masksToBounds = YES;
    leftMarkView.backgroundColor = kAppRedColor;
    [_qh_commentButton addSubview:leftMarkView];
    [leftMarkView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.and.bottom.mas_equalTo(_qh_commentButton);
        make.height.mas_equalTo(2);
    }];
    
    _qh_reviewButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _qh_reviewButton.backgroundColor = kWhiteColor;
    _qh_reviewButton.titleLabel.font = kDefaultFont;
    [_qh_reviewButton setTitle:@"我评价的" forState:UIControlStateNormal];
    [_qh_reviewButton setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    [_qh_reviewButton addTarget:self action:@selector(displayReviewView:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_qh_reviewButton];
    [_qh_reviewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_qh_commentButton.mas_right);
        make.top.mas_equalTo(_qh_commentButton);
        make.width.mas_equalTo(_qh_commentButton);
        make.height.mas_equalTo(50);
    }];
    
    rightMarkView = [[UIView alloc] init];
    rightMarkView.layer.cornerRadius = 1.0;
    rightMarkView.layer.masksToBounds = YES;
    rightMarkView.backgroundColor = kAppRedColor;
    rightMarkView.hidden = YES;
    [_qh_reviewButton addSubview:rightMarkView];
    [rightMarkView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.and.bottom.mas_equalTo(_qh_reviewButton);
        make.height.mas_equalTo(2);
    }];
}

#pragma mark --TargetMethod
- (IBAction)displayCommentView:(UIButton *)sender {
    
    [_qh_commentModelArray removeAllObjects];
    [_qh_tableView reloadData];
    
    leftMarkView.hidden = NO;
    rightMarkView.hidden = YES;
    
    visible = NO;
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkComment)]) {
        
        [_qh_delegate qh_checkComment];
    }
}

- (IBAction)displayReviewView:(UIButton *)sender {
    
    [_qh_commentModelArray removeAllObjects];
    [_qh_tableView reloadData];
    
    leftMarkView.hidden = YES;
    rightMarkView.hidden = NO;
    
    visible = YES;
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkReview)]) {
        
        [_qh_delegate qh_checkReview];
    }
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _qh_commentModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 110.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    GQHReviewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHReviewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    GQHCommentModel *commentModel = _qh_commentModelArray[indexPath.row];
    
    /// 头像
    NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:commentModel.qh_avatar];
    NSURL *URL = [NSURL URLWithString:URLString];
    [cell.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
    
    cell.qh_nameLabel.text = commentModel.qh_name;
    cell.qh_timeLabel.text = [commentModel.qh_commentTime substringToIndex:10];
    cell.qh_contentLabel.text = commentModel.qh_commentContent;
    
    CGRect frame = CGRectMake(0, 0, 100, 20);
    QHRatingView *ratingView = [[QHRatingView alloc] initWithFrame:frame];
    ratingView.backgroundColor = kClearColor;
    [ratingView showStarWithValue:commentModel.qh_score];
    [cell.qh_ratingView addSubview:ratingView];
    
    cell.qh_scoreLabel.text = [NSString stringWithFormat:@"%.2f 分",commentModel.qh_score];
    
    [cell updateConstraints];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

@end
